package com.mark.web.data.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * @Description: 配置文件给加密的字段解密工具类
 * @Author: Mark
 * @CreateDate: 2018/4/23.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private String[] encryptPropNames = { "db.username", "db.password" };//已经加密的字段

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        if(isEncryptProp(propertyName)){//已经加密
            String  decryptPropertyValue = DESUtil.getDecryptString(propertyValue);
            return decryptPropertyValue;
        }else{
            return propertyValue;
        }
    }

    private boolean isEncryptProp(String propertyName) {
        for(String encryptPropertyName : encryptPropNames){
            if (StringUtils.equals(encryptPropertyName,propertyName)){
                return  true;
            }
        }
        return false;
    }
}
